package com.igeek.boot.config;

import com.igeek.boot.interceptor.LoginInterceptor;
import com.igeek.boot.interceptor.MyInterceptor1;
import com.igeek.boot.interceptor.MyInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author fengqq
 * @Description TODO
 * @Date 2024/8/16  15:29
 */
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //默认配置  静态资源的规则 生效css  js等
        WebMvcConfigurer.super.addResourceHandlers(registry);
        //配置本地图片服务器的映射 --> 如果以后用七牛云/阿里云 无需添加以下配置
        registry.addResourceHandler("/pic/**").addResourceLocations("file:/D:\\133java\\1.SpringBoot\\pic\\");

    }

    //定制 @Bean 替换底层组件MultipartResolver
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        //当前多部件解析器，不仅可以支持POST请求、也可以支持PUT请求 ==>默认只支持post请求，如果是restFul风格 需要支持put请求
        multipartResolver.setSupportedMethods("POST","PUT");
        //限制文件上传的大小
        multipartResolver.setMaxUploadSize(5242880);
        multipartResolver.setDefaultEncoding("UTF-8");
        return multipartResolver;
    }

    //添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       /* registry.addInterceptor(new LoginInterceptor())
                //addPathPatterns 配置拦截路径 main,basic_table,form_layouts.html
                .addPathPatterns("/main.html","/basic_table.html","/form_layouts.html")
                //excludePathPatterns 配置放行路径 /login登录相关的 静态资源
                .excludePathPatterns(
                        Stream.of("/login.html","/login","/logout","/css/**","/fonts/**","/images/**","/js/**")
                                .collect(Collectors.toList())
                );*/
        registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/main.html");
        registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/main.html");
    }
}
